package com.north.light.androidutils.canvas.progressbar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * FileName: ProgressBarView
 * Author: lzt
 * Date: 2022/7/7 9:06
 * 自定义进度条--文字在最右侧显示
 * 禁止了硬件加速
 */
public class ProgressBarView extends View implements ProgressBarViewApi {
    private boolean mInit = false;
    //绘制的信息
    private ProgressBarInfo mDrawInfo;

    private Paint mPaint;

    //控件宽高
    private int mViewWidth, mViewHeight;

    //文字显示偏移量
    private int mTextInterval = 10;
    //重叠绘制模式
    private PorterDuffXfermode mPorterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);

    public ProgressBarView(Context context) {
        super(context);
    }

    public ProgressBarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initParams();
    }

    public ProgressBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initParams();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        initParams();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawInfo(canvas);
    }

    /**
     * 获取字体宽高--px
     */
    private int[] getTextWH(String temp, Paint paint) {
        if (paint == null) {
            return new int[]{0, 0};
        }
        Rect rect = new Rect();
        paint.getTextBounds(temp, 0, temp.length(), rect);
        int height = rect.height();
        int width = rect.width();
        return new int[]{width, height};
    }

    /**
     * 初始化参数
     */
    private void initParams() {
        //禁用硬件加速
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(1);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setTextAlign(Paint.Align.LEFT);
        mPaint.setTextSize(dp2px(12));
    }


    /**
     * 绘制信息
     */
    private void drawInfo(Canvas canvas) {
        if (!mInit || mDrawInfo == null || mPaint == null) {
            return;
        }
        //设置混合模式
        mPaint.setXfermode(null);
        mPaint.setShader(null);
        mPaint.setTypeface(Typeface.DEFAULT);
        canvas.save();
        canvas.translate(0, 0);
        //绘制背景---------------------------------------------------混合模式切除边角
        int radius = dp2px(mDrawInfo.getRadius());
        mPaint.setColor(getResources().getColor(mDrawInfo.getBgColor()));
        canvas.drawRoundRect(0, 0, mViewWidth, mViewHeight, radius, radius, mPaint);
        //绘制进度--计算x轴占比
        mPaint.setXfermode(mPorterDuffXfermode);
        float mDrawProgress = (mViewWidth * 1.0f / 100) * (Math.min(mDrawInfo.getProgress(), 100));
        mPaint.setShader(new LinearGradient(0, 0, mDrawProgress, mViewHeight,
                getResources().getColor(mDrawInfo.getProgressStartColor()),
                getResources().getColor(mDrawInfo.getProgressEndColor()),
                Shader.TileMode.CLAMP
        ));
        RectF rectProgress = new RectF(0, 0, mDrawProgress, mViewHeight);
        canvas.drawRoundRect(rectProgress, radius, radius, mPaint);
        //绘制进度文字---------------------------------------------------
        mPaint.setXfermode(null);
        mPaint.setShader(null);
        String mDrawText = (TextUtils.isEmpty(mDrawInfo.getText())) ? "" : mDrawInfo.getText();
        int mDrawTextSize = dp2px(mDrawInfo.getTextSize());
        mPaint.setTextSize(mDrawTextSize);
        mPaint.setColor(getResources().getColor(mDrawInfo.getTextColor()));
        mPaint.setTypeface((mDrawInfo.isTextBold()) ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);
        //字体宽度---------------------------------------------------
        int[] mDrawTextParams = getTextWH(mDrawText, mPaint);
        int mDrawTextHeightInterval = ((mViewHeight - mDrawTextParams[1]) / 2);
        int mDrawTextX = mTextInterval;
        if (mDrawProgress - mDrawTextParams[0] - mTextInterval > 0) {
            mDrawTextX = (int) (mDrawProgress - mDrawTextParams[0] - mTextInterval);
        }
        canvas.drawText(mDrawText, mDrawTextX,
                mViewHeight - mDrawTextHeightInterval, mPaint);
        canvas.restore();
    }


    private int dp2px(int dpValue) {
        return (int) getContext().getResources().getDisplayMetrics().density * dpValue;
    }


    //外部调用方法---------------------------------------------------------------------------------

    @Override
    public void setProgress(ProgressBarInfo info) {
        //view初始化后才设置
        this.postDelayed(new Runnable() {
            @Override
            public void run() {
                mDrawInfo = info;
                mInit = true;
                mViewWidth = getMeasuredWidth();
                mViewHeight = getMeasuredHeight();
                postInvalidate();
            }
        }, 200);
    }


}
